/**   
 * @Title: PlayActivity.java 
 * @Package android.framework.media 
 * @Description: TODO
 * @author jtf@163.com   
 * @date 2013-7-19 下午4:36:56 
 * @version V1.0   
 */
package android.framework.media;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.framework.global.Constants;
import android.framework.media.bean.ShackVideoInfo;
import android.framework.media.hls.HlsPlayController;
import android.framework.media.octo.OctoPlayController;
import android.framework.util.L;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;

/**
 * @ClassName: PlayActivity
 * @Description: TODO
 * @author jtf19@163.com
 * @date 2013-7-19 下午4:36:56
 * 
 */
public class PlayActivity extends BasePlayActivity {

	private static PlayActivity instance;
	private boolean isFromPush = false; // 是否是推送过来的节目
	private boolean isRegKeyguardReceiver = false;
	protected BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			if ("android.intent.action.SCREEN_OFF".equals(intent
					.getAction()) ) {
				if (PlayActivity.this.playController.getVideoView() != null)
					PlayActivity.this.playController.getVideoView().pause();
			} else if ("android.intent.action.SCREEN_ON".equals(intent
					.getAction())//屏幕
					|| "android.intent.action.USER_PRESENT".equals(intent
							.getAction())) {//解锁 
				if (PlayActivity.this.playController.getVideoView() != null)
					PlayActivity.this.playController.getVideoView().start();
			}

		}
	};
	private PlayController playController;

	public static void close() {
		if (instance == null)
			return;
		instance.finish();
		instance = null;
	}

	public static PlayActivity getInstance() {
		return instance;
	}

	@Override
	public void doBack() {
		// TODO Auto-generated method stub
		this.playController.onFinish();
		if (this.playController instanceof HlsPlayController)
			((HlsPlayController) this.playController).onFinish();
		else if (this.playController instanceof OctoPlayController) {
			((OctoPlayController) this.playController).onFinish();
		} else if (this.isFromPush) {
			finish();
		}
	}

	@Override
	public PlayController getPlayController() {
		// TODO Auto-generated method stub
		return null;
	}

	public ShackVideoInfo getVideoInfo() {
		if (this.playController != null)
			return this.playController.getVideoInfo();
		return null;
	}

	@Override
	protected void onActivityResult(int paramInt1, int paramInt2,
			Intent intent) {
		if (this.playController == null)
			return;
		this.playController.onActivityResult(paramInt1, paramInt2, intent);
	}

	protected void onDestroy() {
		super.onDestroy();
		this.playController.onDestroy();
		instance = null;
	}

	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		if (!intent.getBooleanExtra("isThreeScreen", false))
			return;
		// this.playController = new ThreeScreenPlayController(this);
	}

	protected void onPause() {
		super.onPause();
		this.playController.onPause();
	}

	protected void onResume() {
		super.onResume();
		this.playController.onResume();
	}

	protected void onStart() {
		registerKeyguardReceiver();
		super.onStart();
	}

	protected void onStop() {
		unregisterKeyguardReceiver();
		
		super.onStop();
	}

	public boolean onTouchEvent(MotionEvent paramMotionEvent) {
		// if (this.playController instanceof HlsPlayController)
		// return ((HlsPlayController) this.playController)
		// .onTouchEvent(paramMotionEvent);
		// else if (this.playController instanceof OctoPlayController)
		// return ((OctoPlayController) this.playController)
		// .onTouchEvent(paramMotionEvent);
		return super.onTouchEvent(paramMotionEvent);
	}

	protected void registerKeyguardReceiver() {
		if (this.isRegKeyguardReceiver)
			return;
		IntentFilter localIntentFilter = new IntentFilter();
		localIntentFilter.addAction("android.intent.action.SCREEN_ON");
		localIntentFilter.addAction("android.intent.action.SCREEN_OFF");
		localIntentFilter.addAction("android.intent.action.USER_PRESENT");
		registerReceiver(this.mBroadcastReceiver, localIntentFilter);
		this.isRegKeyguardReceiver = true;
	}

	protected void unregisterKeyguardReceiver() {
		if (!this.isRegKeyguardReceiver)
			return;
		unregisterReceiver(this.mBroadcastReceiver);
		this.isRegKeyguardReceiver = false;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		instance = this;
		Bundle bundle = getIntent().getBundleExtra(Constants.PLAY_PARM);
		this.isFromPush = bundle.getBoolean(Constants.FROM_PUSH, false);
		this.play_type = bundle.getInt(Constants.PLAY_TYPE, 0);

		if (this.play_type == Constants.PLAY_OCTO) {
			this.playController = new OctoPlayController(this);
		} else if (this.play_type == Constants.PLAY_HLS) {
			this.playController = new HlsPlayController(this);
		}
		playController.onCreate(bundle);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (keyCode) {
		case KeyEvent.KEYCODE_VOLUME_DOWN:
			if(playController.getMediaController() != null){
				playController.getMediaController().volumeDown();
				playController.getMediaController().adjustVolumeSeekBar();
				playController.getMediaController().adjustSoundDrawable();
			}
			return true;
		case KeyEvent.KEYCODE_VOLUME_UP:
			if(playController.getMediaController() != null){
				playController.getMediaController().volumeUp();
				playController.getMediaController().adjustVolumeSeekBar();
				playController.getMediaController().adjustSoundDrawable();
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
